home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Misc / emu / fbzx.lha / fbzx / Z80.h < prev   
C/C++ Source or Header  |  2004-08-26  |  8KB  |  168 lines

  1. /** Z80: portable Z80 emulator *******************************/
  2. /**                                                         **/
  3. /**                           Z80.h                         **/
  4. /**                                                         **/
  5. /** This file contains declarations relevant to emulation   **/
  6. /** of Z80 CPU.                                             **/
  7. /**                                                         **/
  8. /** Copyright (C) Marat Fayzullin 1994-2002                 **/
  9. /**     You are not allowed to distribute this software     **/
  10. /**     commercially. Please, notify me, if you make any    **/   
  11. /**     changes to this file.                               **/
  12. /*************************************************************/
  13. #ifndef Z80_H
  14. #define Z80_H
  15. #define MSB_FIRST
  16.                                /* Compilation options:       */
  17. /* #define DEBUG */            /* Compile debugging version  */
  18. /* #define LSB_FIRST */        /* Compile for low-endian CPU */
  19. /* #define MSB_FIRST */        /* Compile for hi-endian CPU  */
  20.  
  21.                                /* LoopZ80() may return:      */
  22. #define INT_RST00   0x00C7     /* RST 00h                    */
  23. #define INT_RST08   0x00CF     /* RST 08h                    */
  24. #define INT_RST10   0x00D7     /* RST 10h                    */
  25. #define INT_RST18   0x00DF     /* RST 18h                    */
  26. #define INT_RST20   0x00E7     /* RST 20h                    */
  27. #define INT_RST28   0x00EF     /* RST 28h                    */
  28. #define INT_RST30   0x00F7     /* RST 30h                    */
  29. #define INT_RST38   0x00FF     /* RST 38h                    */
  30. #define INT_IRQ     INT_RST38  /* Default IRQ opcode is FFh  */
  31. #define INT_NMI     0xFFFD     /* Non-maskable interrupt     */
  32. #define INT_NONE    0xFFFF     /* No interrupt required      */
  33. #define INT_QUIT    0xFFFE     /* Exit the emulation         */
  34.  
  35.                                /* Bits in Z80 F register:    */
  36. #define S_FLAG      0x80       /* 1: Result negative         */
  37. #define Z_FLAG      0x40       /* 1: Result is zero          */
  38. #define H_FLAG      0x10       /* 1: Halfcarry/Halfborrow    */
  39. #define P_FLAG      0x04       /* 1: Result is even          */
  40. #define V_FLAG      0x04       /* 1: Overflow occured        */
  41. #define N_FLAG      0x02       /* 1: Subtraction occured     */
  42. #define C_FLAG      0x01       /* 1: Carry/Borrow occured    */
  43.  
  44.                                /* Bits in IFF flip-flops:    */
  45. #define IFF_1       0x01       /* IFF1 flip-flop             */
  46. #define IFF_IM1     0x02       /* 1: IM1 mode                */
  47. #define IFF_IM2     0x04       /* 1: IM2 mode                */
  48. #define IFF_2       0x08       /* IFF2 flip-flop             */
  49. #define IFF_EI      0x20       /* 1: EI pending              */
  50. #define IFF_HALT    0x80       /* 1: CPU HALTed              */
  51.  
  52. /** Simple Datatypes *****************************************/
  53. /** NOTICE: sizeof(byte)=1 and sizeof(word)=2               **/
  54. /*************************************************************/
  55. #ifndef BYTE_TYPE_DEFINED
  56. #define BYTE_TYPE_DEFINED
  57. typedef unsigned char byte;
  58. #endif
  59. #ifndef WORD_TYPE_DEFINED
  60. #define WORD_TYPE_DEFINED
  61. typedef unsigned short word;
  62. #endif
  63. typedef signed char offset;
  64.  
  65. /** Structured Datatypes *************************************/
  66. /** NOTICE: #define LSB_FIRST for machines where least      **/
  67. /**         signifcant byte goes first.                     **/
  68. /*************************************************************/
  69. typedef union
  70. {
  71. #ifdef LSB_FIRST
  72.   struct { byte l,h; } B;
  73. #else
  74.   struct { byte h,l; } B;
  75. #endif
  76.   word W;
  77. } pair;
  78.  
  79. typedef struct
  80. {
  81.   pair AF,BC,DE,HL,IX,IY,PC,SP;       /* Main registers      */
  82.   pair AF1,BC1,DE1,HL1;               /* Shadow registers    */
  83.   byte IFF,I;                         /* Interrupt registers */
  84.   byte R;                             /* Refresh register    */
  85.   byte R2;                            /* Upper bit for R */
  86.  
  87.   int IPeriod,ICount; /* Set IPeriod to number of CPU cycles */
  88.                       /* between calls to LoopZ80()          */
  89.   int TStates;
  90.   int IBackup;        /* Private, don't touch                */
  91.   word IRequest;      /* Set to address of pending IRQ       */
  92.   byte IAutoReset;    /* Set to 1 to autom. reset IRequest   */
  93.   byte TrapBadOps;    /* Set to 1 to warn of illegal opcodes */
  94.   word Trap;          /* Set Trap to address to trace from   */
  95.   byte Trace;         /* Set Trace=1 to start tracing        */
  96.   void *User;         /* Arbitrary user data (ID,RAM*,etc.)  */
  97. } Z80;
  98.  
  99. /** ResetZ80() ***********************************************/
  100. /** This function can be used to reset the registers before **/
  101. /** starting execution with RunZ80(). It sets registers to  **/
  102. /** their initial values.                                   **/
  103. /*************************************************************/
  104. void ResetZ80(register Z80 *R);
  105.  
  106. /** ExecZ80() ************************************************/
  107. /** This function will execute a single Z80 opcode. It will **/
  108. /** then return next PC, and current register values in R.  **/
  109. /*************************************************************/
  110. word ExecZ80(register Z80 *R);
  111.  
  112. /** IntZ80() *************************************************/
  113. /** This function will generate interrupt of given vector.  **/
  114. /*************************************************************/
  115. void IntZ80(register Z80 *R,register word Vector);
  116.  
  117. /** RunZ80() *************************************************/
  118. /** This function will run Z80 code until an LoopZ80() call **/
  119. /** returns INT_QUIT. It will return the PC at which        **/
  120. /** emulation stopped, and current register values in R.    **/
  121. /*************************************************************/
  122. word RunZ80(register Z80 *R);
  123.  
  124. /** RdZ80()/WrZ80() ******************************************/
  125. /** These functions are called when access to RAM occurs.   **/
  126. /** They allow to control memory access.                    **/
  127. /************************************ TO BE WRITTEN BY USER **/
  128. void WrZ80(register word Addr,register byte Value);
  129. byte RdZ80(register word Addr);
  130.  
  131. /** InZ80()/OutZ80() *****************************************/
  132. /** Z80 emulation calls these functions to read/write from  **/
  133. /** I/O ports. There can be 65536 I/O ports, but only first **/
  134. /** 256 are usually used                                    **/
  135. /************************************ TO BE WRITTEN BY USER **/
  136. void OutZ80(register word Port,register byte Value);
  137. byte InZ80(register word Port);
  138.  
  139. /** PatchZ80() ***********************************************/
  140. /** Z80 emulation calls this function when it encounters a  **/
  141. /** special patch command (ED FE) provided for user needs.  **/
  142. /** For example, it can be called to emulate BIOS calls,    **/
  143. /** such as disk and tape access. Replace it with an empty  **/
  144. /** macro for no patching.                                  **/
  145. /************************************ TO BE WRITTEN BY USER **/
  146. void PatchZ80(register Z80 *R);
  147.  
  148. /** DebugZ80() ***********************************************/
  149. /** This function should exist if DEBUG is #defined. When   **/
  150. /** Trace!=0, it is called after each command executed by   **/
  151. /** the CPU, and given the Z80 registers. Emulation exits   **/
  152. /** if DebugZ80() returns 0.                                **/
  153. /*************************************************************/
  154. #ifdef DEBUG
  155. byte DebugZ80(register Z80 *R);
  156. #endif
  157.  
  158. /** LoopZ80() ************************************************/
  159. /** Z80 emulation calls this function periodically to check **/
  160. /** if the system hardware requires any interrupts. This    **/
  161. /** function must return an address of the interrupt vector **/
  162. /** (0x0038, 0x0066, etc.) or INT_NONE for no interrupt.    **/
  163. /** Return INT_QUIT to exit the emulation loop.             **/
  164. /************************************ TO BE WRITTEN BY USER **/
  165. word LoopZ80(register Z80 *R);
  166.  
  167. #endif /* Z80_H */
  168.